import DList from "@/components/dlist";
export default {
  data () {
    return {
      loading: false,
      finished: false,
      page: 1,
      americanFans:[],
      statistics:{},
      active:0,
      rankingList:[
        'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/gold_medal.png',
        'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/silver_medal.png',
        'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/bronze_medal.png'
      ],
      current_member:false,
    };
  },
  mounted () {
    this.onLoad();
    this.getStatistics();
  },
  methods: {
    // 获取美粉值统计
    async getStatistics() {
      let { data, result, msg } = await $http.post("plugin.powder-value.frontend.stat.index");
      if (result === 0) {
        this.$toast(msg);
        return;
      }
      this.statistics = data;
    },
    async getRewardList(){
      if (this.loading === true || this.finished === true) {
        return;
      }
      this.loading = true;
      let url = this.active ? "plugin.powder-value.frontend.stat.detailed" : "plugin.powder-value.frontend.stat.ranks"
      let { data, result, msg } = await $http.post(url,{page: this.page});
      if (result === 0) {
        this.$toast(msg);
        return;
      }
      if(!this.active) {
        if (data.ranks.data.length === 0 || data.ranks.data.length < data.ranks.per_page || data.ranks.current_page === data.ranks.last_page) {
          this.finished = true;
        }
        this.americanFans.push(...data.ranks.data);
        if(data.current_member.constructor === Object && this.page == 1) {
          this.americanFans.unshift(data.current_member);
          this.current_member = true;
        }
        for(let item of this.americanFans) {
          item.member.username = item.member.username.slice(0,2) + '****';
        }
      }else {
        if (data.data.length === 0 || data.data.length < data.per_page || data.current_page === data.last_page) {
          this.finished = true;
        }
        this.americanFans.push(...data.data);
      }
      this.page++;
      this.loading = false;
    },
    onLoad(){
      this.getRewardList();
    },
    // 切换tabs
    changeTabs(value) {
      this.loading = false;
      this.finished = false;
      this.page = 1;
      this.americanFans = [];
      this.getRewardList();
    }
  },
  components: {
    DList
  },
};